package com.databasteknik.krustykookiez;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;


public class Search extends JPanel{
	private static final long serialVersionUID = 1;
	private static final int NBR_FIELDS = 9;
	private String[] searchNames = {"Barcode", "Recipe", "Status", "Customer", "Order Id","Production date start","Production date stop", "Delivery date start" ,"Delivery date stop"};
	private String[] resultNames = {"Barcode", "Recipe", "Status", "Customer", "Order Id","Production date","Delivery date"};
	private Database db;
	private JTextField[] fields;
	private DefaultTableModel tableModel;
	
	public Search(Database db){
		this.db = db;
		setLayout(new BorderLayout());
		add(createTopPanel(), BorderLayout.PAGE_START);
		add(createButtons(), BorderLayout.CENTER);
		add(createBottomPanel(), BorderLayout.CENTER);
	}
	
	public JComponent createTopPanel() {
		JPanel top = new JPanel();
		String[] texts = new String[NBR_FIELDS];
		for(int i = 0; i < texts.length; i++){
			texts[i] = searchNames[i];
		}
		
		fields = new JTextField[NBR_FIELDS];
		for (int i = 0; i < fields.length; i++) {
			fields[i] = new JTextField(20);
			fields[i].setEditable(true);
		}
		JPanel left = new JPanel();
		left.setLayout(new GridLayout(texts.length, 1));
		for (int i = 0; i < texts.length; i++) {
			JLabel label = new JLabel(texts[i] + "      ", JLabel.RIGHT);
			left.add(label);
		}
		JPanel right = new JPanel();
		right.setLayout(new GridLayout(fields.length, 1));
		for (int i = 0; i < fields.length; i++) {
			right.add(fields[i]);
		}

		JPanel searchField = new JPanel(new BorderLayout());
		searchField.add(left, BorderLayout.LINE_START);
		searchField.add(right, BorderLayout.CENTER);
		top.setLayout(new BorderLayout());
		top.add(searchField, BorderLayout.PAGE_START);
		top.add(createButtons(), BorderLayout.LINE_END);
		return top;
	}
	
	public JComponent createButtons(){
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		JButton searchButton = new JButton("Search");
		JButton clearButton = new JButton("Clear");
		searchButton.addActionListener(new SearchListener());
		clearButton.addActionListener(new ClearListener());
		buttonPanel.add(clearButton, BorderLayout.CENTER);
		buttonPanel.add(searchButton, BorderLayout.LINE_END);
		return buttonPanel;
	}
	
	public JComponent createBottomPanel(){
		tableModel = new DefaultTableModel(resultNames, 0) {
			private static final long serialVersionUID = 1L;
			@Override
			public boolean isCellEditable(int row, int column){
				return false;
			}
		};
		JTable table = new JTable(tableModel);
		JScrollPane bottomPane = new JScrollPane(table);
		return bottomPane;
	}
	
	private void clearFields(){
		for (int i = 0; i < fields.length; i++) {
			fields[i].setText("");
		}
	}
	
	class SearchListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			boolean empty = true;
			String[] values = new String[NBR_FIELDS];
			for(int i = 0; i < values.length; i++){
				values[i] = fields[i].getText();
				if(!values[i].equals("")){
					empty = false;
				}
			}
			tableModel.setNumRows(0);
			if(!empty){
				ArrayList<String[]> result = db.search(values);
				for(String[] s : result){
					tableModel.insertRow(tableModel.getRowCount(), s);
				}				
			}
		}
		
	}
	
	class ClearListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			clearFields();
		}
		
	}

}
